﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ServiceModel;
using RilattackLibrary;

namespace RilattackGameWPF
{
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            serveurEndpoint.Text = "net.tcp://" + System.Configuration.ConfigurationManager.AppSettings["ServerDefaultIP"] + ':' + System.Configuration.ConfigurationManager.AppSettings["ServerDefaultPort"] + "/RilAttack";
            NickName.Text = "Player";
            App.Window = this;
        }

        private void connexionBtn_Click(object sender, RoutedEventArgs e)
        {
            EndpointAddress endpoint = new EndpointAddress(serveurEndpoint.Text);
            App.Client = new CallBack();
            DuplexChannelFactory<IServer> factory = new DuplexChannelFactory<IServer>(App.Client, new NetTcpBinding(SecurityMode.None), endpoint);
            try
            {
                App.Server = factory.CreateChannel();
                if (App.Server != null)
                {
                    if (!App.Server.login(NickName.Text))
                    {
                        MessageBox.Show("Le pseudo est deja utilisé !");
                    }
                    else
                    {
                        App.Client.CurrentPlayer = new ControlablePlayer();
                        App.Client.CurrentPlayer.Nickname = NickName.Text;
                        App.Client.CurrentPlayer.UID = App.Server.getMyUID();
                        connexionBtn.IsEnabled = false;
                    }
                }
            }
            catch
            {
                MessageBox.Show("Impossible de se connecter au serveur !");
            }
        }
    }
}
